<!doctype HTML>
<?php

$page = $_SERVER['PHP_SELF'];
$sec = "3";
?>
<html>
    <head>
        <title>Kitchen</title>
        <link type="text/css" rel="stylesheet" href="stylesheet.css">
		<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
            <style>
                .orderItem{
                    width: 300px;
                    height: 500px;
                    background-color:"grey";
                    border-style: solid;
                    border-width: 3px;
                    overflow: auto;
                }
                .orderItem .start{
                    background-color:"yellow";
                }
                .orderItem .complete{
                    background-color:"green";
                }
        
            </style>
                
                
    </head>

    <script src="js/jquery-1.11.0.min.js"></script>
    <script src="js/jquery.mousewheel.js"></script>
    <script>
        $(function(){
            $("#kitchenWrapper").wrapInner("<table cellspacing='30'><tr>");
            $(".orderItem").wrap("<td>");
            $("body").mousewheel(function(event, delta) {
			this.scrollLeft -= (delta * 30);
			event.preventDefault();
            });
            //Color changing script; with current page refresh timer, will
            //not remain on page for long enough to matter
            /*$("#start").submit(function(){
                $(this).parent().css("background-color", "yellow");    
            });
*/            //Color changing script for complete buton; change is not 
            //since page reloads every 5 seconds
            /*$("#complete").submit(function() {
                $(this).parent().css("background-color", "green");   
            });*/
            //Sandwich started script
            //$("#start").submit(function() {
            //    <?php
            //        echo "<div style=\"float:right; text-align: right\">
            //        In-Progress
            //        </div>"
            //    ?>
            //}
        });
    </script>

    <style>
        tr {
            vertical-align: top;     
        }
        
        
    </style>
    <body>
        <h1 class="title">Elliphino's Sandwich Shop</h1>
        
        <div id="kitchenWrapper">
            
			<?php 
					session_start();
                   
                    if($_SESSION['managerHelp']==1)
                    {
                        ?>
                        <script>
                            alert("Customer is aquiring manager assisstance");
                        </script>
                        <?php
                    
                    }
                    if($_SESSION['staffHelp']==1)
                    {
                        ?>
                        <script>
                            alert("Customer is aquiring manager assisstance");
                        </script>
                        <?php
                    }
             

                    $_SESSION['i'] = 0;
                    $username = 'root';
					$pw = 'password';

                  //Connect to the database.
                    $con = mysqli_connect ("localhost", $username, $pw, 'elliphinos');

                    if (mysqli_connect_errno()) 
					{
                         echo "Failed to connect to MySQL: " . mysqli_connect_error();
                    }
					
					if ( isset($_SESSION['order_id']) ){
						$idToRemove = $_SESSION['order_id'];
						$sql = "DELETE FROM orders WHERE order_id = $idToRemove";
						mysqli_query($con, $sql);
					}
					
                    $sql = "SELECT * FROM orders";
                    $result = mysqli_query($con, $sql);
					$row = mysqli_fetch_array($result);
					if ( !mysqli_query($con, $sql) ) 
					{
						die( 'Error: ' . mysqli_error($con) );
					}
                    //create array that dynamicall stores 
					$numRows = mysqli_num_rows($result);
                    echo $numRows-1;
                   
            
					for ($i = 0; $i < $numRows - 1; $i++)
					{
						$array = mysqli_fetch_array($result, MYSQLI_NUM);
                        
						echo "<div class=\"orderItem\"><span>ORDER " . $array[0] . ": " . $array[1] . "</span></br>";
                         
						for ($j = 2; $j < 11; $j++) 
						{
                            
							switch ($j) {
								case 2:
									echo "<b>Bread: </b>";
									break;
								case 3:
									echo "<b>Meat: </b>";
									break;
								case 4:
									echo "<b>Cheese: </b>";
									break;
								case 5:
									echo "<b>Veggies: </b>";
									break;
								case 6:
									echo "<b>Condiments: </b>";
									break;
								case 7:
									echo "<b>Drink: </b>";
									break;
								case 8:
									echo "<b>Chips: </b>";
									break;
								case 9:
									echo "<b>Dessert: </b>";
									break;
								default:
									break;
							}
							
							echo $array[$j];
							echo "</br>";
                              
						}
						$buttonName = "delete" . $array[0];
						$buttonName2 = "start" . $array[0];
						$buttonName3 = "end" . $array[0];
                        //create new array...$buttonName2=>$_SESSION['start']
						?>
							
						<form id="start" method='POST' action="">
							<input type="submit" value="START" name = "<?php echo $buttonName2; ?>" >
                                <?php
                                     
                                    if(isset($_POST[$buttonName2]))
                                    {
                                         $location = substr($buttonName2, 5);
                                        
                                        $_SESSION['start'] = 1; //1 is identified in array as start
                                     
                                        if(isset($_SESSION['kitchenArray']))
                                        {
                                           
                                             $_SESSION['kitchenArray'][$location] =  $_SESSION['start'];
            
                                
                                
                                             if($location == 1)
                                            {
                                                echo $_SESSION['count']++;
                                                echo $i . "=" . "Started" . "</br>";
                                        
                                            }
                                          
                                        }
                                        else
                                            $_SESSION['kitchenArray'] = array($location => $_SESSION['start']); 
                                        //set $_SESSION flag to 1
                                        //if flag is 1 display hello world
                                       
                                    }
                                ?>
                            </form>
							</br>
                        <form id="complete" method='POST' action="">
							<input type="submit" value="COMPLETE" name="<?php echo $buttonName3; ?>" >
                               <?php 
                                if(isset($_POST[$buttonName3]))
                                {   //$_SESSION['start'] = 0; //if sandwich is completed then complete is true and start is false
                                    //echo $buttonName3;
                                    $location = substr($buttonName3, 3); //trim off "complete" to get only order number
                                    $_SESSION['complete'] = 2; //2 is identified in array as complete
                                    //using sub class and super classes could fix this!!
                                    if(isset($_SESSION['kitchenArray']))
                                    {
                                           
                                        $_SESSION['kitchenArray'][$location] =  $_SESSION['complete']; 
                                        //echo $_SESSION['kitchenArray'][$location];
                                        
                                    }
                                    else
                                        $_SESSION['kitchenArray'] = array($location => $_SESSION['complete']); 
                                    
                                }
                                       
                                        
                                ?>
                            </form>
							</br>
                        <form id="delete" method='POST' action="">
							<input type="submit" value="DELETE" name="<?php echo $buttonName; ?>" >
							<?php
								if (isset($_POST[$buttonName]) ) 
								{
                                    $location = substr($buttonName, 6);
                                    unset($_SESSION['kitchenArray'][$location]);
                                    var_dump($_SESSION['kitchenArray']);
                                    //$_SESSION['start']=0; //if delete then start and complete are set to false
                                   // $_SESSION['complete']=0;
                                    
									$_SESSION['order_id'] = $array[0];
                                    //array push order id
								}
                              
								?>
						</form>
						      
						<?php
                      
						echo "</div>";
                        
                }
                      if(isset($_SESSION['kitchenArray']))
                        {
                            //echo $_SESSION['order_id'];
                            print_r($_SESSION['kitchenArray']);
                            foreach($_SESSION['kitchenArray'] as $i)
                            {
                            
                                ?>
                                    <script>
                                         $(".orderItem"<?php echo "$i";  ?>).wrap("<td>");
                                    </script>
                                
                                   <?php
                            }
                            foreach($_SESSION['kitchenArray'] as $i=>$value)
                            {
                                if($value == 2)
                                {
                                    //set this background to green
                                    //have order number...make yellow with  this order number
                                    echo $i . "=" . "Completed" . "</br>";
                                    ?>
                                    <script>
                                        $('.orderItem').css({"background-color":"green"});
                                    </script>
                                    
                                <?php
                                }
                                else if($value == 1)
                                {
                                    echo $_SESSION['count']++;
                                    echo $i . "=" . "Started" . "</br>";
                                    ?>
                                    <script>
                                           $('.orderItem').css({"background-color":"yellow"});
                                    </script>
                                    
                                <?php
                                }
                                else if($value == NULL){
                                    ?>
                                    <script>
                                        $('.orderItem').css({"background-color":"grey"});
                                    </script>
                                <?php
                                    //set this background to yellow
                            }
                        }
                      }
                                          
                 
                             
                        
     
			?>
        </div>
        
    </body>
</html>